package com.xr.blog.controller;

import com.xr.blog.pojo.BlogCategory;
import com.xr.blog.service.BlogCategoryService;
import com.xr.blog.tools.Result;
import com.xr.blog.tools.constant.Code;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分类表
 *
 * @author XRJ
 * @email 1033835209@qq.com
 * @date 2020-07-07 20:31:38
 */
@RestController
@RequestMapping("/squirrelblog")
public class BlogCategoryController {
    @Autowired
    private BlogCategoryService blogCategoryService;

    @GetMapping(value = "/category/getById",produces = "application/json;charset=UTF-8")
    public Result<BlogCategory> info(Long id){
        return Result.of(Code.SC_OK.getState(),"查询成功",blogCategoryService.getById(id));
    }

    @GetMapping(value = "/category/getList",produces = "application/json;charset=UTF-8")
    public Result<List<BlogCategory>> list(){
        return Result.of(Code.SC_OK.getState(),"查询成功",blogCategoryService.list());
    }


    @PostMapping(value = "/category/addOrUpdate",produces = "application/json;charset=UTF-8")
    public Result save(@RequestBody @Validated({Update.class, Insert.class}) BlogCategory category){
        //ValidatorUtils.validate(category);
        blogCategoryService.saveOrUpdate(category);
        return Result.of(Code.SC_OK.getState(),"操作成功",category);
    }

    @DeleteMapping(value = "/category/delete",produces = "application/json;charset=UTF-8")
    public Result remove(Long id){
        return blogCategoryService.removeById(id)?Result.of(Code.SC_OK.getState(),"删除成功",null):Result.of(Code.DEL_FAIL.getState(),"删除失败",null);
    }

}
